let checkInOrder1 = getData('checkInOrder')
let maxmiddle = 0 //最大页
let nowmiddle = 0  //当前
// // 渲染分页按钮
// let middlenums = Math.ceil(checkInOrder1.length / 10)
// maxmiddle = middlenums
// for (let i = 1; i <= middlenums; i++) {
//     $('.hears').before(`<li class='myli'><a href="#">${i}</a></li>`)
// }
$('#typeOption').click(function () {
    let searchType1 = $(this).val()
    // console.log(searchType);
    if (searchType1 == 'option1') {
        $('#OptionType1').attr('placeholder', '请输入姓名')
    }
    else if (searchType1 == 'option2') {
        $('#OptionType1').attr('placeholder', '请输入房号')
    }
})

// 给每一个分页按钮绑定点击事件
$('.pagination').on('click', '.myli', function () {
    let clickNum = $(this).text()
    nowmiddle = clickNum
    console.log(nowmiddle);
    let start = (parseInt(clickNum) - 1) * 10  //起始下标
    let end = start + 9  //终止下标
    if (clickNum == maxmiddle) {
        end = checkInOrder1.length - 1  //数据最后一项的下标
    }
    showTbody(start, end)
})
let zhuan = []
let hedr1=[]
function koko() {
    for (let b = 0; b < checkInOrder1.length; b++) {
        let zhauntai = checkInOrder1[b].orderState
        if (zhauntai === '已完成') {
            zhuan.push(checkInOrder1[b])
        }
        if(zhauntai==='已取消'){
            hedr1.push(checkInOrder1[b])
        }
    }
}
koko()
console.log(zhuan,hedr1);

//渲染页面数据(已完成)
let time = getTime()
// console.log(time);

function showTbody() {
    $('#mytbody').html('')
    for (let h = 0; h < zhuan.length; h++) {
        // console.log(123);
        let htmlStr1 = ` 
<tr>
    <td>${zhuan[h].clientName}</td>
    <td>${zhuan[h].roomNumber}</td>
    <td>${zhuan[h].price}</td>
    <td>${zhuan[h].cashPledge}</td>
    <td>${zhuan[h].outtime}</td>
    <td>${zhuan[h].orderState}</td>
    <td>${zhuan[h].cashPledge}</td>
    <td>${zhuan[h].inputter}</td>
    <td class='jkie'>         
        <button class="tankuang">详情</button>
    </td>
</tr>
`
        $('#mytbody').append(htmlStr1)
        
    }
}
showTbody()
function showTbody1() {
    // $('#mytbody').html('')
    for (let k = 0; k < hedr1.length; k++) {
        // console.log(123);
        let htmlStr1 = ` 
<tr>
    <td>${hedr1[k].clientName}</td>
    <td>${hedr1[k].roomNumber}</td>
    <td>${hedr1[k].price}</td>
    <td>${hedr1[k].cashPledge}</td>
    <td>${hedr1[k].outtime}</td>
    <td>${hedr1[k].orderState}</td>
    <td>${hedr1[k].cashPledge}</td>
    <td>${hedr1[k].inputter}</td>
    <td class='jkie'>         
        <button class="tankuang">详情</button>
    </td>
</tr>
`
        $('#mytbody').append(htmlStr1)
        // }
        // }
    }
}
showTbody1()

//遍历数组中的房费
let num = []
let sum = 0  //累加求和
function shuju() {
    for (let j = 0; j < checkInOrder1.length; j++) {
        let aggregate = parseInt(checkInOrder1[j].price)
        let cash = parseInt(checkInOrder1[j].cashPledge)
        num.push(aggregate)
        num.push(cash)
    }
    return num
}
shuju()

// 计算总房费+押金
function zongheji() {
    for (let k = 0; k < num.length; k++) {
        sum = sum + num[k]
    }
    return sum
}
zongheji()
console.log(sum);
//
let jing = []  //房费数组
let shouru = 0  //累加求和
function fanfeisr() {
    for (let j = 0; j < zhuan.length; j++) {
        let aggregate = parseInt(zhuan[j].cashPledge)
        jing.push(aggregate)
    }
    return jing
}
fanfeisr()
console.log(jing);
//押金


function jingheji() {
    for (let l = 0; l < jing.length; l++) {
        shouru = shouru + jing[l]
    }
    return shouru
}
jingheji()
//已取消的房费和押金
let xujin=[]
let cee=0
function quxiaofei() {
    for(let x=0;x<hedr1.length;x++){
        let dhii= parseInt(hedr1[x].price)
        let dhii2 = parseInt(hedr1[x].cashPledge)
        xujin.push(dhii)
        xujin.push(dhii2)
        
    }
    return xujin
}
quxiaofei()
console.log(xujin);
function box(){
    for (let i = 0; i < xujin.length; i++) {
        cee = cee + xujin[i]
    }
    return cee
}
box()
console.log(cee);
let money3 = sum
let money1 = sum - (shouru+cee)
let money2 = shouru+cee

//遍历数组中的押金
//显示总收入
$('.table-responsive').on('click', '#count', function () {
    $('#mytbody1').html('')
    let htmlStr = ` 
    <tr>
        <td>${money3}</td>
        <td>${money2}</td>
        <td>${money1}</td>
    </tr>
    `
    $('#mytbody1').append(htmlStr)
})
$('#shousuo').on('click', function () {
    //清除表单中按钮的默认提交事件
    // $('#table2').show()
    event.preventDefault()
    let shujuval = $(this).siblings('div').children('input').val()
    let fanhaoval = $(this).siblings('div').children('input').val()
    for (let f = 0; f < zhuan.length; f++) {
        if (shujuval === zhuan[f].clientName ||fanhaoval===zhuan[f].roomNumber ) {
            // console.log(zhuan[f]);
            let htmlStr1 = ` 
            <tr>
                <td>${zhuan[f].clientName}</td>
                <td>${zhuan[f].roomNumber}</td>
                <td>${zhuan[f].price}</td>
                <td>${zhuan[f].cashPledge}</td>
                <td>${zhuan[f].outtime}</td>
                <td>${zhuan[f].orderState}</td>
                <td>${zhuan[f].cashPledge}</td>
                <td>${zhuan[f].inputter}</td>
                <td class='jkie'>         
                    <button class="tankuang">详情</button>
                </td>
            </tr>
            `
            $('#mytbody2').append(htmlStr1)

        }
    }
})
$('#shousuo').on('click', function () {
    //清除表单中按钮的默认提交事件
    // $('#table2').show()
    event.preventDefault()
    let shujuval = $(this).siblings('div').children('input').val()
    let fanhaoval = $(this).siblings('div').children('input').val()
    for (let f = 0; f < hedr1.length; f++) {
        if (shujuval === hedr1[f].clientName ||fanhaoval===hedr1[f].roomNumber) {
            
            let htmlStr1 = ` 
            <tr>
                <td>${hedr1[f].clientName}</td>
                <td>${hedr1[f].roomNumber}</td>
                <td>${hedr1[f].price}</td>
                <td>${hedr1[f].cashPledge}</td>
                <td>${hedr1[f].outtime}</td>
                <td>${hedr1[f].orderState}</td>
                <td>${hedr1[f].cashPledge}</td>
                <td>${hedr1[f].inputter}</td>
                <td class='jkie'>         
                    <button class="tankuang">详情</button>
                </td>
            </tr>
            `
            $('#mytbody2').append(htmlStr1)

        }
    }
})
//搜索弹框详情
$('#mytbody2').on('click', '.tankuang', function () {
    $('#myModal').modal('show')
    let nameVal = $(this).parents('tr').children('td').eq(0).text()
    let roomVal = $(this).parents('tr').children('td').eq(1).text()
    let priceVal = $(this).parents('tr').children('td').eq(2).text()
    let cashVal = $(this).parents('tr').children('td').eq(3).text()
    // let timeVal = $(this).parents('tr').children('td').eq(4).text()

    $('#checkInOrder-name').val(nameVal)
    $('#checkInOrder-room').val(roomVal)
    $('#checkInOrder-price').val(priceVal)
    $('#checkInOrder-cach').val(cashVal)
    // $('#checkInOrder-time').val(timeVal)
    for (let i = 0; i < zhuan.length; i++) {
        if (nameVal === zhuan[i].clientName) {
            let time1=zhuan[i].outtime
            let order = zhuan[i].orderNumber
            let state = zhuan[i].orderState
            let inputter = zhuan[i].inputter
            // console.log(order);
            $('#checkInOrder-time').val(time1)
            $('#checkInOrder-order').val(order)
            $('#checkInOrder-orderState').val(state)
            $('#checkInOrder-inputter').val(inputter)

        }
    }
})
$('#mytbody2').on('click', '.tankuang', function () {
    $('#myModal').modal('show')
    let nameVal = $(this).parents('tr').children('td').eq(0).text()
    let roomVal = $(this).parents('tr').children('td').eq(1).text()
    let priceVal = $(this).parents('tr').children('td').eq(2).text()
    let cashVal = $(this).parents('tr').children('td').eq(3).text()
    // let timeVal = $(this).parents('tr').children('td').eq(4).text()

    $('#checkInOrder-name').val(nameVal)
    $('#checkInOrder-room').val(roomVal)
    $('#checkInOrder-price').val(priceVal)
    $('#checkInOrder-cach').val(cashVal)
    // $('#checkInOrder-time').val(timeVal)
    for (let i = 0; i < hedr1.length; i++) {
        if (nameVal === hedr1[i].clientName) {
            let time1=hedr1[i].outtime
            let order = hedr1[i].orderNumber
            let state = hedr1[i].orderState
            let inputter = hedr1[i].inputter
            // console.log(order);
            $('#checkInOrder-time').val(time1)
            $('#checkInOrder-order').val(order)
            $('#checkInOrder-orderState').val(state)
            $('#checkInOrder-inputter').val(inputter)

        }
    }
})
//上一页
// $('#fronts').click(function () {
//     //根据当前页来判断
//     let num = parseInt(nowmiddle) - 1
//     let start = (parseInt(num) - 1) * 10  //起始下标
//     let end = start + 9   //终止下标

//     //判断当前点击的是不是第一页
//     if (nowmiddle == 1) {

//     } else {
//         showTbody(start, end)
//         nowmiddle = num   //修改完数据过后，当前页要更新
//     }
// })
// //下页面
// $('#back').click(function () {
//     //根据当前页来判断
//     let num = parseInt(nowmiddle) + 1   //num 是应该渲染的是哪一页
//     console.log(num);
//     let start = (parseInt(num) - 1) * 10  //起始下标
//     let end = start + 9   //终止下标
//     //判断当前点击的是不是最后一页，是最后一页的话 end值就要变
//     if (num == maxmiddle) {
//         end = checkInOrder1.length - 1  //数据最后一项的下标
//     }
//     console.log(start, end);

//     //判断当前点击的是不是最后一页
//     if (nowmiddle == maxmiddle) {

//     } else {
//         showTbody(start, end)
//         nowmiddle = num   //修改完数据过后，当前页要更新
//     }
// })
//详情
$('#mytbody').on('click', '.tankuang', function () {
    $('#myModal').modal('show')
    let nameVal = $(this).parents('tr').children('td').eq(0).text()
    let roomVal = $(this).parents('tr').children('td').eq(1).text()
    let priceVal = $(this).parents('tr').children('td').eq(2).text()
    let cashVal = $(this).parents('tr').children('td').eq(3).text()
    let timeVal = $(this).parents('tr').children('td').eq(4).text()

    $('#checkInOrder-name').val(nameVal)
    $('#checkInOrder-room').val(roomVal)
    $('#checkInOrder-price').val(priceVal)
    $('#checkInOrder-cach').val(cashVal)
    $('#checkInOrder-time').val(timeVal)
    for (let i = 0; i < checkInOrder1.length; i++) {
        if (nameVal === checkInOrder1[i].clientName) {
            let order = checkInOrder1[i].orderNumber
            let state = checkInOrder1[i].orderState
            let inputter = checkInOrder1[i].inputter
            // console.log(order);
            $('#checkInOrder-order').val(order)
            $('#checkInOrder-orderState').val(state)
            $('#checkInOrder-inputter').val(inputter)

        }
    }
})
// 显示当前登录者头像及工号
showHeadImgId()

// 头部退出按钮
$('#quit').find('button').click(function () {
    open('./login.html', '_self')
})

//显示当前登录者头像及工号
function showHeadImgId() {
    let useData = getData('staff')
    let loginUser = getData('loginUser').jobNumber
    for (let i = 0; i < useData.length; i++) {
        if (useData[i].jobNumber == loginUser) {
            $('.portrait').find('img').attr('src', useData[i].headPortrait)
            $('#quit').find('.jobId').html(useData[i].jobNumber)
        }

    }
}
let AccessLevel = getData('loginUser').AccessLevel
console.log(AccessLevel);
if (AccessLevel > 1) {
    insufficientPrivileges()
}
//权限不足提示
function insufficientPrivileges() {
    //展示权限不足框
    $('#insufficientPrivileges').show().siblings().hide()
    //清除原来的显示数据
    $('#insufficientPrivileges').html('')
    //展示权限不足
    $('#insufficientPrivileges').eq(0).append(`
 <p>权限不足请联系管理员</p>
`)
}

//点击搜索按钮
$('button').on('click', function () {
    //清除表单中按钮的默认提交事件
    event.preventDefault()
})

//点击主菜单
$('#mianNav .navbar-nav>li').on('click', function () {
    //设置自身为选中状态并清除其他选项的选中状态
    $(this).addClass('mianActive').siblings().removeClass('mianActive')
})

//点击二级菜单
$('main .navbar-nav>li').on('click', function () {
    //设置自身为选中状态并清除其他选项的选中状态
    $(this).addClass('active').siblings().removeClass('active')
})